package theeg.DependencyAnalysis.Core.Algorithms.SubstringDetection;

public class Suffix {

    public Node originNode;
    public int beginIndex;
    public int endIndex;

    public Suffix(Node originNode, int beginIndex, int endIndex) {
        this.originNode = originNode;
        this.beginIndex = beginIndex;
        this.endIndex = endIndex;
    }

    public boolean IsExplicit() {
        return beginIndex > endIndex;
    }

    public void Canonize() {
        if (IsExplicit()) {
            return;
        }
        Edge edge = originNode.findEdge(originNode.atIndex(beginIndex));

        int edgeSpan = edge.getSpan();
        while (edgeSpan <= getSpan()) {
            beginIndex += edgeSpan + 1;
            originNode = edge.endNode;
            if (IsExplicit()) {
                continue;
            }
            edge = edge.endNode.findEdge(originNode.atIndex(beginIndex));
            edgeSpan = edge.getSpan();
        }
    }

    public int getSpan() {
        return endIndex - beginIndex;
    }

    public void changeOriginNode() {
        originNode = originNode.suffixNode;
    }
}
